package beastick.configurator;

//tofigurator Open Source Project
//
//Copyright 2008 Tofig Kareemov, estereos@gmail.com
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

import java.math.BigInteger;
import java.util.Date;

import beastick.configurator.types.CParameterizedString;
import beastick.configurator.types.CTypedMap;
import beastick.utility.CFileTools;
import beastick.utility.CUtilityMethods;

public final class SConfigurator extends CUtilityMethods
{
 private final static CConfigurator oConfigurator = new CConfigurator();
 private static boolean             bReload       = false;
 private static String              sConfMainFile = "";

 public static void ConfigurationManagerOnChange()
 {
  if (GetBoolean("beastick.utility.SConfigurator.bReload"))
  {
   Initialize(sConfMainFile);
   SetProtectedString("beastick.utility.SConfigurator.bReload", "Done");
  }
 }

 public static void Initialize(String isConfMainFile)
 {
  sConfMainFile = isConfMainFile;
  oConfigurator.Load(sConfMainFile);
  GetHome();
  Report();
 }

 public static void Report()
 {
  String sSConfiguratorReportInclude = GetString("system.property.SConfiguratorReportInclude");
  String sSConfiguratorReportExclude = GetString("system.property.SConfiguratorReportExclude");
  if (IsValuable(sSConfiguratorReportInclude))
  {
   String[] sInclude =
   { sSConfiguratorReportInclude };
   String[] sExclude =
   { sSConfiguratorReportExclude };
   ExportToXMLFile(sInclude, sExclude, System.getProperty("user.home", "./")
     + "/conf/temp/SConfiguratorReport.xml",
     CConfigurator.I_EXPORT_FILE_TYPE_LIST);
  }
 }

 public static Object GetSynchronizer()
 {
  return oConfigurator;
 }

 public static void Reload()
 {
  synchronized (GetSynchronizer())
  {
   oConfigurator.Reload();
  }
 };

 public static void ImportFromXMLFile(String isFileName, boolean bSilently)
 {
  synchronized (GetSynchronizer())
  {
   oConfigurator.ImportFromXMLFile(isFileName, bSilently);
  }
 };

 public static Object GetInstanceOf(String sClassName)
 {
  Object oResult = null;
  try
  {
   oResult = GetInstanceOf(Class.forName(sClassName));
  } catch (Throwable T)
  {
   T.printStackTrace();
  }
  return oResult;
 }

 public static Object GetInstanceOf(Object oObject)
 {
  Object oResult = null;
  try
  {
   oResult = GetInstanceOf(oObject.getClass());
  } catch (Throwable T)
  {
   T.printStackTrace();
  }
  return oResult;
 }

 public static Object GetInstanceOf(Class oClass)
 {
  Object oResult = null;
  try
  {
   oResult = oClass.newInstance();
   SetMyFields(oResult);
  } catch (Throwable T)
  {
   T.printStackTrace();
  }
  return oResult;
 }

 public static boolean ParameterExists(String sParameter)
 {
  return oConfigurator.ParameterExists(sParameter);
 }

 public static void SetMyFields(Object ioCaller)
 {
  if (CUtilityMethods.IsValuable(ioCaller))
   synchronized (ioCaller)
   {
    oConfigurator.SetMyFields(ioCaller);
   }
 };

 public static void SetMyInstanceFields(Object ioCaller, String sInstanceName)
 {
  if (CUtilityMethods.IsValuable(ioCaller))
   synchronized (ioCaller)
   {
    oConfigurator.SetMyInstanceFields(ioCaller, sInstanceName);
   }
 };

 public static void SetMyInstanceFieldsExplicitly(Object ioCaller,
   String sExplicitClassFullName)
 {
  if (CUtilityMethods.IsValuable(ioCaller))
   synchronized (ioCaller)
   {
    oConfigurator.SetMyInstanceFieldsExplicitly(ioCaller,
      sExplicitClassFullName);
   }
 };

 public static void SetMySingleField(Object ioCaller, String sFieldName)
 {
  if (CUtilityMethods.IsValuable(ioCaller))
   synchronized (ioCaller)
   {
    oConfigurator.SetMySingleField(ioCaller, sFieldName);
   }
 };

 public static boolean SetMySingleFieldExplicitly(Object ioCaller,
   String sFieldName, String sParameterName)
 {
  boolean bResult = false;
  if (CUtilityMethods.IsValuable(ioCaller))
   synchronized (ioCaller)
   {
    bResult = oConfigurator.SetMySingleFieldExplicitly(ioCaller, sFieldName,
      sParameterName);
   }
  return bResult;
 };

 public static Object GetValueAsObject(Object ooObject, String sParameterName)
 {
  return oConfigurator.GetValueAsObject(ooObject, sParameterName);
 };

 public static String GetString(String sParameterName)
 {
  return (String) oConfigurator.GetValueAsObject(new String(), sParameterName);
 };

 public static void SetString(String sParameterName, String sParameterValue)
 {
  synchronized (GetSynchronizer())
  {
   oConfigurator.SetValueAsString(sParameterName, sParameterValue);
  }
 };

 public static void SetProtectedString(String sParameterName,
   String sParameterValue)
 {
  synchronized (GetSynchronizer())
  {
   oConfigurator.SetProtectedValueAsString(sParameterName, sParameterValue);
  }
 };

 public static void RemoveParameter(String sParameterName)
 {
  synchronized (GetSynchronizer())
  {
   oConfigurator.RemoveParameter(sParameterName);
  }
 };

 public static CParameterizedString GetParameterizedString(String sParameterName)
 {
  return (CParameterizedString) oConfigurator.GetValueAsObject(
    new CParameterizedString(), sParameterName);
 };

 public static CTypedMap GetTypedMap(String sParameterName)
 {
  String sParamValue = GetString(sParameterName);
  if (CUtilityMethods.IsValuable(sParamValue))
  {
   return (new CTypedMap(sParamValue, oConfigurator));
  } else
  {
   return (new CTypedMap(sParameterName, oConfigurator));
  }
 };

 public static String GetPrivateString(String sParameterName)
 {
  return (String) oConfigurator.GetPrivateValueAsString(sParameterName);
 };

 public static Object GetObjectArray(Class oType, String sParameterName)
 {
  return oConfigurator.GetObjectArray(oType, sParameterName);
 }

 public static int GetInt(String sParameterName)
 {
  return oConfigurator.GetValueAsInt(sParameterName);
 };

 public static long GetLong(String sParameterName)
 {
  return ((Long) oConfigurator.GetValueAsObject(new Long(0), sParameterName))
    .longValue();
 };

 public static double GetDouble(String sParameterName)
 {
  return ((Double) oConfigurator
    .GetValueAsObject(new Double(0), sParameterName)).doubleValue();
 }

 public static float GetFloat(String sParameterName)
 {
  return ((Float) oConfigurator.GetValueAsObject(new Float(0), sParameterName))
    .floatValue();
 }

 public static boolean GetBoolean(String sParameterName)
 {
  return oConfigurator.GetValueAsBoolean(sParameterName);
 }

 public static BigInteger GetBigInteger(String sParameterName)
 {
  return (BigInteger) oConfigurator.GetValueAsObject(new BigInteger("0"),
    sParameterName);
 }

 public static Date GetDate(String sParameterName)
 {
  return (Date) oConfigurator.GetValueAsObject(new Date(), sParameterName);
 }

 public static String[] GetStringArray(String sParameterName)
 {
  String[] oArray = new String[GetInt(sParameterName)];
  for (int i = 0; i < oArray.length; ++i)
  {
   oArray[i] = GetString(sParameterName + "." + i);
  }
  return oArray;
 }

 public static boolean ExportToXMLFile(String[] sIncludePattern,
   String[] sExcludePattern, String sFileName, int iType)
 {
  synchronized (GetSynchronizer())
  {
   oConfigurator.SetValueAsString("0.0.ConfigurationSize", ""
     + oConfigurator.GetConfigurationSize());
   return oConfigurator.ExportToXMLFile(sIncludePattern, sExcludePattern,
     sFileName, iType);
  }
 }

 public static boolean isRunningUnderApplicationServer()
 {
  return GetBoolean("beastick.generic_configuration.runtime_flags.bRunningUnderApplicationServer");
 }

 public static String GetHome()
 {
  boolean bToRefresh = false;
  String sResult = GetString("system.property.home");
  if (!IsValuable(sResult))
  {
   bToRefresh = true;
   sResult = GetPrivateString("system.property.home");
  }
  if (!IsValuable(sResult))
  {
   bToRefresh = true;
   sResult = "./";
  }
  if (bToRefresh)
  {
   SetString("system.property.home", (new CFileTools())
     .FindFileFullName(sResult));
  }
  return sResult;
 }

 public static String GetUserHome()
 {
  boolean bToRefresh = false;
  String sResult = GetString("system.property.User_home");
  if (!IsValuable(sResult))
  {
   bToRefresh = true;
   sResult = GetString("system.property.user.home");
  }
  if (!IsValuable(sResult))
  {
   bToRefresh = true;
   sResult = GetPrivateString("system.property.user.home");
  }
  if (!IsValuable(sResult))
  {
   bToRefresh = true;
   sResult = "./";
  }
  if (bToRefresh)
  {
   SetString("system.property.User_home", sResult);
  }
  return sResult;
 }
}